package com.platform.modules.quality.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.util.DocUtil;
import com.platform.modules.quality.service.QlyInspectionService;
import com.platform.modules.quality.dto.QlyInspectionListByAppDto;
import com.platform.modules.quality.entity.QlyInspectionEntity;
import com.platform.modules.quality.entity.QlyInspectionItemEntity;
import com.platform.modules.quality.entity.QlyInspectionItemReformEntity;
import com.platform.modules.quality.service.QlyInspectionItemReformService;
import com.platform.modules.quality.service.QlyInspectionItemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author biao
 * @date 2020-02-18 16:30:00
 */
@Api(tags = "InspectionItemReformController|质量巡查反馈")
@RestController
@RequestMapping("qly/inspection/reform")
public class QlyInspectionItemReformController extends AbstractController {

    @Autowired
    private QlyInspectionService qlyInspectionService;
    @Autowired
    private QlyInspectionItemService qlyInspectionItemService;
    @Autowired
    private QlyInspectionItemReformService qlyInspectionItemReformService;

    /**
     * @descr 保存/修改 问题整改记录<br>
     * @param qlyInspectionEntity
     * @return
     */
    @ApiOperation(value = "保存/修改 - 问题整改记录")
    @PostMapping("save")
    public RestResponse save(@RequestBody QlyInspectionItemReformEntity qlyInspectionItemReformEntity) {
    	//判断是否更新
    	if(StringUtil.isNotEmpty(qlyInspectionItemReformEntity.getInsItemReformId())) {
    		qlyInspectionItemReformService.save(qlyInspectionItemReformEntity);
    	}else {
    		//创建
    		//判断是否已存在复查状态为空或者 1-合格的整改记录
    		Boolean exist = qlyInspectionItemReformService.isExist(qlyInspectionItemReformEntity);
    		if(!exist) {
    			//保存整改记录并更新问题状态
    			qlyInspectionItemReformService.saveAndUpdateInspectionStatus(qlyInspectionItemReformEntity);
    		}else {
    			return RestResponse.error("该问题存在未复查的整改记录");
    		}
    	}
        return RestResponse.success().put("data", qlyInspectionItemReformEntity);
    }
    
    /**
     * @descr 保存/修改 整改复查结果<br>
     * @param qlyInspectionEntity
     * @return
     */
    @ApiOperation(value = "保存 - 整改复查结果")
    @PostMapping("/review/save")
    public RestResponse reviewSave(@RequestBody QlyInspectionItemReformEntity qlyInspectionItemReformEntity) {
		//保存整改记录并更新问题状态
		qlyInspectionItemReformService.saveReviewAndUpdateInspectionStatus(qlyInspectionItemReformEntity, getUserId());
        return RestResponse.success().put("data", qlyInspectionItemReformEntity);
    }
    
}
